<head>
    <link href="${ctx}/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <link href="${ctx}/font-awesome/3.0.0/css/font-awesome.min.css" rel="stylesheet">
    <link href="${ctx}/css/index.css" rel="stylesheet">
    <link href="${ctx}/bootstrap-table/1.12.0/bootstrap-table.min.css" rel="stylesheet">
    <script src="${ctx}/jquery/2.1.4/jquery.min.js"></script>
    <script src="${ctx}/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="${ctx}/bootstrap-table/1.12.0/bootstrap-table.min.js"></script>
    <script src="${ctx}/bootstrap-table/1.12.0/locale/bootstrap-table-zh-CN.min.js"></script>
    <script src="${ctx}/bootstrap-table/1.12.0/extensions/export/bootstrap-table-export.min.js"></script>
    <script src="${ctx}/js/extends.js"></script>
    <script src="${ctx}/layui/layui.all.js"></script>
    <script src="${ctx}/js/util.js"></script>
    <link href="${ctx}/layui/css/layui.css" rel="stylesheet">
    <style>
        .btn {
            font-size: 12px;
        }

        .layui-form-checkbox[lay-skin=primary] i {
            top: 5px;
        }

        .layui-table-tool {
            background: #fff;
        }
    </style>
</head>
<body>
<title>-- 新手任务管理</title>
<div style="padding-top: 10px">
    <input class="btn btn-success radius" onclick="search()" type="button" value="刷新">
    <a href="javascript:;" id="multiDelete" class="btn btn-danger radius"><i class="Hui-iconfont">&#xe6e2;</i> 批量删除</a>
    <a href="javascript:;" data-id="0" class="btn btn-primary radius gradeEdit"><i class="Hui-iconfont">&#xe600;</i> 添加</a>
</div>

<table id="layTable" lay-filter="tableFilter"></table>
</body>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="save">保存</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<!--请在下方写此页面业务相关的脚本-->
<script type="text/html" id="switchTpl">
    <!-- 这里的 checked 的状态只是演示 -->
    <input type="checkbox" name="sex" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="sexDemo" {{
           d.hasDeleted== 'False' ? 'checked' : '' }}>

</script>

<script type="text/javascript">
    var tableIns;
    var form;
    var ids = "";
    var table = "tb_module_task";
    $(function () {
        layui.use('table', function () {
            tableIns = layui.table,
                form = layui.form;
            tableIns.render({
                elem: '#layTable'
                , url: '/newhand/list'
                , method: 'POST'
                , toolbar: true

                //  ,where:{table:table,query:{'name':'','id':''},time:{'start':startDate,'end':endDate},key:{'name':searchKey}}
                , id: 'dataTableId'
                , cols: [[
                    {type: 'checkbox', field: 'id'}
                    // ,{edit:'text',field:'moduleId', title: '模块id', sort: true, width:60, align: 'center', sort: true}
                    //
                    //  ,{edit:'text',field:'taskId', title: '任务id', sort: true, width:160, align: 'center', sort: true}

                    , {field: 'title', title: '标题', sort: true, width: 260, align: 'center', sort: true}
                    , {field: 'money', title: '金額', sort: true, width: 160, align: 'center', sort: true}
                    , {
                        field: 'category',
                        title: '任务类型',
                        sort: true,
                        width: 160,
                        align: 'center',
                        sort: true,
                        templet: function (data) {
                            var str = "";
                            switch (data.category) {
                                case "Activity":
                                    str = "活动任务";
                                    break;
                                case "Shared":
                                    str = "分享任务";
                                    break;
                                case "Focus":
                                    str = "关注公众号任务";
                                    break;
                                default:
                                    break;
                            }
                            return str;
                        }
                    }
                    , {field: 'icon', title: '图标', sort: true, width: 160, align: 'center', sort: true}
                    , {edit: 'text', field: 'sort', title: '排序', sort: true, width: 160, align: 'center', sort: true}
                    , {
                        field: 'enable',
                        title: '是否启用',
                        width: 100,
                        align: 'center',
                        sort: true,
                        templet: '#switchTpl',
                        unresize: true
                    }
                    , {
                        field: 'gmt_created', title: '创建时间', templet: function (data) {
                            return dateFormat(data.createTime);
                        }, align: 'center', width: 200, sort: true
                    }
                    , {
                        field: 'gmt_modified', title: '修改时间', templet: function (data) {
                            return dateFormat(data.modifyTime);
                        }, align: 'center', width: 200, sort: true
                    }
                    , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 250, align: 'center'}

                ]]
                , page: true
            });


            tableIns.on('checkbox(tableFilter)', function (obj) {
                var checkStatus = tableIns.checkStatus('dataTableId');
                var dataList = checkStatus.data;
                if (dataList != null && dataList.length > 0) {

                    for (var i = 0; i < dataList.length; i++) {
                        ids += dataList[i].id + ",";
                    }
                }
            });
            form.on('switch(sexDemo)', function (obj) {
                // layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
                if (obj.elem.checked) {
                    commonenable(this.value, 'False');
                } else {
                    commonenable(this.value, 'True');
                }

            });

            function deleteId(ids) {
                if (ids != "") {
                    $.ajax({
                        url: "/newhand/delete",
                        type: 'post',
                        data: {"table": table, "ids": ids},
                        async: false,
                        dataType: "json",
                        success: function (arg) {
                            if (arg) {
                                layer.msg(arg.message);
                                search();
                            } else {
                                layer.msg(arg.message);
                            }
                        }
                    });
                } else {
                    layer.msg("请勾选数据")
                }
            }

            $("#multiDelete").click(function () {
                deleteId(ids)
            });

            tableIns.on('tool(tableFilter)', function (obj) {
                var data = obj.data;
                if (obj.event === 'detail') {
                    layer.msg('ID：' + data.id + ' 的查看操作');
                } else if (obj.event === 'del') {
                    if (data.has_deleted == 1) {
                        layer.msg('已删除，不可进行该操作!', {icon: 1, time: 1000});
                        return;
                    }
                    layer.confirm('真的删除行么', function (index) {
                        deleteId(data.id)
                        //layer.close(index);
                    });
                } else if (obj.event === 'edit') {
                    if (data.category === 'Activity') {
                        layer_show("修改活动任务", "/newhand/edit?id=" + data.id + "&category=" + data.category, "550", "550");
                    }
                    if (data.category === 'Shared') {
                        layer_show("修改分享朋友圈任务", "/newhand/edit?id=" + data.id + "&category=" + data.category, "550", "650");
                    }
                    if (data.category === 'Focus') {
                        layer_show("修改关注公众号任务", "/newhand/edit?id=" + data.id + "&category=" + data.category, "550", "410");
                    }

                } else if (obj.event === 'save') {
                    var data2 = {id: data.id, sort: data.sort}
                    $.ajax({
                        url: "/newhand/sortsave",    //请求的url地址
                        dataType: "json",   //返回格式为json
                        async: true,//请求是否异步，默认为异步，这也是ajax重要特性
                        data: data2,    //参数值,键值对
                        type: "post",   //请求方式
                        beforeSend: function () {
                            //请求前的处理
                        },
                        success: function (req) {
                            //请求成功时处理
                            if (req.success) {
                                showmsg(req.message);
                                search();
                            } else {
                                layer.msg(req.message)
                            }
                        },
                        complete: function () {
                            //请求完成的处理
                        },
                        error: function () {
                            //请求出错处理
                            layer.msg("请重试！若在失败请联系管理员")
                        }
                    });
                }
            });
        });
    });

    function search() {

        tableIns.reload('dataTableId', {
            page: {}
        });
    }


    $(document).ready(function () {
        $(".gradeEdit").click(function () {
            layer.confirm('请选择新增任务的类型', {
                btn: ['活动任务', '分享朋友圈', '关注公众号'] //可以无限个按钮
                , btn3: function (index, layero) {
                    //按钮【按钮三】的回调
                    layer.close(index);
                    layer_show("新增关注公众号任务", "/newhand/edit?category=Focus", "550", "460");
                }
            }, function (index, layero) {
                layer.close(index);
                //按钮【按钮一】的回调
                layer_show("新增活动任务", "/newhand/edit?category=Activity", "550", "600");
            }, function (index) {
                layer.close(index);
                //按钮【按钮二】的回调
                layer_show("新增分享朋友圈任务", "/newhand/edit?category=Shared", "550", "600");
            });

        });

    });


    function showmsg(msg) {
        layer.msg(msg)
    }

    function commonenable(id, status) {
        $.ajax({
            url: "/newhand/enablebyid",
            type: 'post',
            data: {"table": table, "id": id, "status": status},
            async: false,
            dataType: "json",
            success: function (arg) {
                if (arg) {
                    layer.msg(arg.message);
                    search();
                } else {
                    layer.msg(arg.message);
                }
            }
        });
    }
</script>
<!--/请在上方写此页面业务相关的脚本-->
